package com.limeng.sys.controller;

import com.limeng.common.Result;
import com.limeng.sys.entity.Role;
import com.limeng.sys.entity.User;
import com.limeng.sys.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author limeng
 * @since 2023-03-27
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/all")
    public Result<List<Role>> getAll(){
        List<Role> list = roleService.list();
        return Result.success(list);
    }

    /**
     * 新增角色接口
     * @param role
     * @return
     */
    @PostMapping
    public Result<?> addRole(@RequestBody Role role){
        roleService.save(role);
        return Result.success("新增用户成功");
    }

    /**
     * 修改角色接口
     * @param role
     * @return
     */
    @PutMapping
    public Result<?> updateRole(@RequestBody Role role){
        roleService.updateById(role);
        return Result.success("修改角色成功");
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteRoleById(@PathVariable("id") Integer id){
        roleService.removeById(id);
        return Result.success("删除角色成功");
    }

    /**
     * 角色回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Role> getRoleById(@PathVariable("id") Integer id){
        Role role = roleService.getById(id);
        return Result.success(role);
    }

}
